﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace Keepo.Helpers
{
    public class StringHelper
    {
        public static string FromHebrewToGibrish(string str)
        {
            if (str == null)
            {
                return "";
            }

            //get string with hebrew characters and convert the hebrew characters to Gibrish characters
            string newstr = "";
            for (int i = 0; i < str.Length; i++)
            {
                newstr += ((int)str[i]).ToString() + ",";
            }
            return newstr.Substring(0, newstr.Length - 1);
        }

        public static string FromGibrishToHebrew(string str)
        {
            //revert back to hebrew characters
            string newstr = "";
            string[] parts = str.Split(',');
            foreach (string part in parts)
            {
                int number = 0;
                if (int.TryParse(part, out number))
                {
                    newstr += ((char)(int.Parse(part))).ToString();
                }
            }
            return newstr;
        }

        /// <summary>
        /// Change the string to the state that the user can't identify it (Password encryption)
        /// </summary>
        public static string Encrypt(string str)
        {
            string newStr = "";
            List<char> listChar = new List<char>();
            str = str.Substring(0, 2) + "t" + str.Substring(2);
            char[] arr = str.ToCharArray();
            foreach (char tav in arr)
            {
                listChar.Add(tav);
            }
            listChar.Reverse();
            foreach (char tav in listChar)
            {
                newStr += tav.ToString();
            }
            return newStr;
        }

        /// <summary>
        /// Revert the changed string to the original state that the user can identify it (Password decryption)
        /// </summary>
        public static string Decrypt(string str)
        {
            char[] arr = str.ToCharArray();
            string newStr = "";
            List<char> listChar = new List<char>();
            foreach (char tav in arr)
            {
                listChar.Add(tav);
            }
            listChar.Reverse();
            for (int i = 0; i < listChar.Count; i++)
            {
                if (i != 2)
                {
                    newStr += listChar[i].ToString();
                }
            }
            return newStr;
        }

        /// <summary>
        /// Get string with lineBreaks (\r\n) and change these to html lineBreaks (</br>)
        /// </summary>
        public static string ReplaceBR(string str)
        {
            return str.Replace("\r\n", "<br />").Replace("\n", "<br />");
        }

        public static string ConvertLinks(string text)
        {
            //convert the url's without [URL] tags (just "http://www.walla.co.il") into <a href> tag
            text = Regex.Replace(text, @"()(http://\S+)(\s+|$)", "<a href='$2' target='_blank'>$2</a>$3");
            return text;
        }

        /// <summary>
        /// get string with BBcode (forum code) and convert these tags to HTML ([B] will be <b>)
        /// </summary>
        /// <param name="str","</param>
        /// <returns></returns>
        public static string BBcode(string text)
        {
            text = ConvertLinks(text);

            text = ReplaceBR(text);

            //replace simple tags
            text = text.Replace("[B]", "<b>");
            text = text.Replace("[/B]", "</b>");
            text = text.Replace("[U]", "<u>");
            text = text.Replace("[/U]", "</u>");
            text = text.Replace("[I]", "<i>");
            text = text.Replace("[/I]", "</i>");
            text = text.Replace("[b]", "<b>");
            text = text.Replace("[/b]", "</b>");
            text = text.Replace("[u]", "<u>");
            text = text.Replace("[/u]", "</u>");
            text = text.Replace("[i]", "<i>");
            text = text.Replace("[/i]", "</i>");
            text = text.Replace("[center]", "<center>");
            text = text.Replace("[/center]", "</center>");
            text = text.Replace("[CENTER]", "<center>");
            text = text.Replace("[/CENTER]", "</center>");
            text = text.Replace("[quote]", "<table><tr><td style='border:1px black solid;' width='500'>");
            text = text.Replace("[/quote]", "</td></tr></table>");
            text = text.Replace("[QUOTE]", "<table><tr><td style='border:1px black solid;' width='500'>");
            text = text.Replace("[/QUOTE]", "</td></tr></table>");
            text = text.Replace("[color", "[COLOR");
            text = text.Replace("[/color", "[/COLOR");
            text = text.Replace("[size", "[SIZE");
            text = text.Replace("[/size", "[/SIZE");
            text = text.Replace("[img-noresize", "[IMG-NORESIZE");
            text = text.Replace("[/img-noresize", "[/IMG-NORESIZE");
            text = text.Replace("[img-small", "[IMG-SMALL");
            text = text.Replace("[/img-small", "[/IMG-SMALL");
            text = text.Replace("[img", "[IMG");
            text = text.Replace("[/img", "[/IMG");
            text = text.Replace("[url", "[URL");
            text = text.Replace("[/url", "[/URL");
            text = text.Replace("IMG-NORESIZE", "IMG-SMALL");
            text = text.Replace("[youtube", "[YOUTUBE");
            text = text.Replace("[/youtube", "[/YOUTUBE");
            text = text.Replace("=\"", "=");
            text = text.Replace("\"]", "]");

            //convert the [IMG]link[/IMG] into HTML <img> (small image that with click you make it bigger)
            text = Regex.Replace(text, @"\[IMG\](.*?)\[/IMG\]", @"<a href='$1' target='_blank'><img style='border:0px;style:300px;' src='$1'></img></a>");

            //convert the [IMG-SMALL]link[/IMG-SMALL] into HTML <img> (just the image, without make it bigger with click)
            text = Regex.Replace(text, @"\[IMG-SMALL\](.*?)\[/IMG-SMALL\]", @"<img src='$1' style='border:0px' />");

            //convert the [YOUTUBE]link[/YOUTUBE] into HTML <object>
            text = Regex.Replace(text, @"\[YOUTUBE\](\S+)\[/YOUTUBE\]", "<object width=\"445\" height=\"364\"><param name=\"movie\" value=\"http://www.youtube.com/v/$1&hl=en&fs=1&color1=0x2b405b&color2=0x6b8ab6&border=1\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/$1&hl=en&fs=1&color1=0x2b405b&color2=0x6b8ab6&border=1\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"445\" height=\"364\"></embed></object>");

            //convert the [COLOR=orange]text[/COLOR] into HTML <span>
            text = Regex.Replace(text, @"(\[COLOR=)([^\]]*?)(\])(.*?)(\[/COLOR\])", @"<span style='color:$2;'>$4</span>");

            //convert the [SIZE=7]text[/SIZE] into HTML <font>
            text = Regex.Replace(text, @"(\[SIZE=)([^\]]*?)(\])(.*?)(\[/SIZE\])", @"<font size='$2'>$4</font>");

            //convert the [URL=pic]dfg[/URL] into HTML <a>
            text = Regex.Replace(text, @"(\[URL=)([^\]]*?)(\])(.*?)(\[/URL\])", @"<a href='$2' target='_blank'>$4</a>");

            return text;
        }

        public static string SiteURL(UrlHelper Url)
        {
            string url = Url.Action("Index", "Home", null, "http");
            if (!url.Contains("www") && !url.Contains("localhost"))
            {
                url = url.Replace("http://", "http://www.");
            }

            return url;
        }
    }
}